package com.alumni_circle.controller.home;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.PageBase;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.utils.ModelUtils;
import com.alumni_circle.core.utils.PictureBase64;
import com.alumni_circle.core.utils.SecurityUtils;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.home.ClubActivity;
import com.alumni_circle.entity.home.VO.ClubActivityInsertVO;
import com.alumni_circle.entity.home.VO.ClubActivityUpdateVO;
import com.alumni_circle.entity.home.VO.ClubActivityViewVO;
import com.alumni_circle.entity.home.issue.UserClub;
import com.alumni_circle.entity.me.User;
import com.alumni_circle.service.home.IClubActivityService;
import com.alumni_circle.service.home.issue.IUserClubService;
import com.alumni_circle.utils.MsgPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

import static com.alumni_circle.core.constant.ResponseCode.PERMISSIONS;

/**
 * @author 龙小虬
 * @since 2020-07-06 22:58
 */
@Api(tags = "社团管理")
@Controller
@RequestMapping("/clubActivity")
public class ClubActivityController extends BaseController<ClubActivity> {


    @Resource
    private IClubActivityService clubActivityService;

    @Resource
    private IUserClubService userClubService;

    @Resource
    MsgPermissions msgPermissions;

    /**
     * 根据标题、内容、地点、时间、图片、token增加社团活动文章
     * @param clubActivityInsertVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addClubActivity", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "新增社团活动信息", notes = "增加社团活动信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="增",accessInfo="新增社团活动信息",url="/clubActivity/addClubActivity")
    public ResponseModel<java.io.Serializable> addClubActivity(@ApiParam(required = true, name = "clubActivityInsertVO")
                                         @RequestBody ClubActivityInsertVO clubActivityInsertVO) {
        
        //返回数据为全部图片的路径，现需要去除第一张照片
        String path = "";
        if(!StrUtil.hasEmpty(clubActivityInsertVO.getPicture()) && clubActivityInsertVO.getPicture().contains("{-temporary}")) {
           path = clubActivityInsertVO.getPicture();
           clubActivityInsertVO.setPicture("");
        }
        User user = SecurityUtils.getUser();
        ClubActivity clubActivity = ModelUtils.transform(clubActivityInsertVO,ClubActivity.class);
        clubActivity.setClubName(user.getClub());
        clubActivity.setCreateBy(user.getId());
        clubActivity.setCreateName(user.getName());
        //保存文章
        clubActivityService.save(clubActivity);
        super.picturePath(1,clubActivity.getId(),path);
        UserClub userClub = new UserClub();
        userClub.setUserId(user.getId());
        userClub.setClubActivityId(clubActivity.getId());
        userClubService.save(userClub);
        return new ResponseModel<>("success");
    }

    /**
     * 根据文章id更新活动的点赞量、浏览量
     * @param clubActivityUpdateVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateClubActivity", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新社团活动信息", notes = "更新社团活动信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新社团活动信息",url="/clubActivity/updateClubActivity")
    public ResponseModel<String> updateClubActivity(@ApiParam(required = true, name = "ClubActivityUpdateVO")
                                            @RequestBody ClubActivityUpdateVO clubActivityUpdateVO) {

        if(!ObjectUtil.hasEmpty(clubActivityUpdateVO.getPraise()) || !ObjectUtil.hasEmpty(clubActivityUpdateVO.getViews())){
            //更新流量
            ClubActivity clubActivity = new ClubActivity();
            clubActivity.setId(clubActivityUpdateVO.getId());
            if(!StrUtil.hasEmpty(clubActivityUpdateVO.getPraise())) {
                clubActivity.setPraise(clubActivityUpdateVO.getPraise());
            }
            if (!ObjectUtil.hasEmpty(clubActivityUpdateVO.getViews())){
                clubActivity.setViews(clubActivityUpdateVO.getViews());
            }
            clubActivityService.updateById(clubActivity);
        }else {
            if(msgPermissions.updateDelete(clubActivityUpdateVO.getId(), "club")){
                return new ResponseModel<>(PERMISSIONS.getCode(),PERMISSIONS.getMessage());
            }
            clubActivityService.updateById(ModelUtils.transform(clubActivityUpdateVO,ClubActivity.class));
        }

        return new ResponseModel<>("success");
    }


    /**
     * 根据id来查询数据
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectClubActivity", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询社团活动信息", notes = "查询社团活动信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询社团活动信息",url="/clubActivity/selectClubActivity")
    public ResponseModel<Object> selectClubActivity(@ApiParam(required = true, name = "baseId")
                                            @RequestBody BaseId baseId) {

        ClubActivity clubActivity = clubActivityService.getById(baseId.getId());
        return new ResponseModel<>(ModelUtils.transform(clubActivity, ClubActivityViewVO.class));
    }

    /**
     * 根据关键字查询所有的社团活动文章
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchClubActivity", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询社团活动信息", notes = "查询社团活动信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询社团活动信息",url="/clubActivity/searchClubActivity")
    public ResponseListModel<ClubActivityViewVO> searchClubActivity(@ApiParam(required = true, name = "pageBase")
                                            @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<ClubActivity>list = clubActivityService.list(new QueryWrapper<ClubActivity>().and(wrapper -> wrapper.like("title",pageBase.getContent()).or().like("content",pageBase.getContent())).orderByDesc("create_time"));

        for (ClubActivity clubActivity : list) {

            if (!StrUtil.hasEmpty(clubActivity.getPicture())) {
                clubActivity.setPicture(PictureBase64.thumbnailList(clubActivity.getPicture()));
            }

        }

        return new ResponseListModel<>(ModelUtils.copyListProperties(
                list,ClubActivityViewVO.class));
    }

    /**
     * 根据关键字搜索
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchViewClubActivity", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询社团活动信息", notes = "查询社团活动信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询社团活动信息",url="/clubActivity/searchViewClubActivity")
    public ResponseListModel<ClubActivityViewVO> searchViewClubActivity(@ApiParam(required = true, name = "pageBase")
                                                @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        return new ResponseListModel<>(clubActivityService.searchClubActivity(SecurityUtils.getClubName(),pageBase.getContent()));
    }


    /**
     * 删除信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteClubActivity", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除社团活动信息", notes = "删除社团活动信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="删",accessInfo="删除社团活动信息",url="/clubActivity/deleteClubActivity")
    public ResponseModel deleteClubActivity(@ApiParam(required = true, name = "baseId")
                                            @RequestBody BaseId baseId) {

        if(!msgPermissions.updateDelete(baseId.getId(), "club","开发社")){
            return new ResponseModel<>(PERMISSIONS.getCode(),PERMISSIONS.getMessage());
        }
        clubActivityService.removeById(baseId.getId());
        QueryWrapper<UserClub> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("club_activity_id",baseId.getId());
        userClubService.remove(queryWrapper);
        super.picturePathDelete(baseId.getId());
        return new ResponseModel();
    }

    /**
     * 查询所有的社团活动信息
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllClubActivity", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询所有社团活动信息", notes = "查询所有社团活动信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询所有社团活动信息",url="/clubActivity/getAllClubActivity")
    public ResponseListModel<ClubActivityViewVO> getAllClubActivity(@ApiParam(required = true, name = "ResponseListModel")
                                                @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        QueryWrapper<ClubActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        List<ClubActivity>list =  clubActivityService.list(queryWrapper);
        PageInfo<ClubActivity> pageInfo = new PageInfo<>(list);
        for (ClubActivity clubActivity : list) {
            if (!StrUtil.hasEmpty(clubActivity.getPicture())) {
                clubActivity.setPicture(PictureBase64.thumbnailList(clubActivity.getPicture()));
            }

        }
        return new ResponseListModel<>(ModelUtils.copyListProperties(list, ClubActivityViewVO.class),String.valueOf(pageInfo.getTotal()));
    }

    /**
     * 查询用户所属社团发布的信息
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectMyClubActivity", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "我的社团内的list信息", notes = "我的社团内的list信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="我的社团内的list信息",url="/clubActivity/selectMyClubActivity")
    public ResponseListModel<Object> selectMyClubActivity(@ApiParam(required = true, name = "ClubActivity")
                                                      @RequestBody PageBase pageBase) {
        QueryWrapper<ClubActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("club_name", SecurityUtils.getClubName());
        queryWrapper.orderByDesc("create_time");
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<ClubActivity> list = clubActivityService.list(queryWrapper);
        for (ClubActivity clubActivity : list) {
            if (!StrUtil.hasEmpty(clubActivity.getPicture())) {
                clubActivity.setPicture(PictureBase64.thumbnailList(clubActivity.getPicture()));
            }

        }
            return new ResponseListModel<>(ModelUtils.copyListProperties(list, ClubActivityViewVO.class));
    }

    /**
     * 查询用户本人发布的社团活动
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectMyListClubActivity", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询社团活动信息", notes = "查询社团活动信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询社团活动信息",url="/clubActivity/selectMyListClubActivity")
    public ResponseListModel<ClubActivityViewVO> selectMyListClubActivity(@ApiParam(required = true, name = "pageBase")
                                                                          @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<ClubActivityViewVO>list = clubActivityService.selectMyListClubActivity(SecurityUtils.getId());
        for (ClubActivityViewVO clubActivityViewVO : list) {
            if (!StrUtil.hasEmpty(clubActivityViewVO.getPicture())) {
                clubActivityViewVO.setPicture(PictureBase64.thumbnailList(clubActivityViewVO.getPicture()));
            }

        }
        return new ResponseListModel<>(list);
    }


    /**
     * 查询用户本人发布的社团活动
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchMyListClubActivity", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询社团活动信息", notes = "查询社团活动信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询社团活动信息",url="/clubActivity/searchMyListClubActivity")
    public ResponseListModel<ClubActivityViewVO> searchMyListClubActivity(@ApiParam(required = true, name = "pageBase")
                                                                          @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<ClubActivityViewVO>list = clubActivityService.searchMyListClubActivity(SecurityUtils.getId(),pageBase.getContent());
        for (ClubActivityViewVO clubActivityViewVO : list) {
            if (!StrUtil.hasEmpty(clubActivityViewVO.getPicture())) {
                clubActivityViewVO.setPicture(PictureBase64.thumbnailList(clubActivityViewVO.getPicture()));
            }

        }
        return new ResponseListModel<>(list);
    }



    @ResponseBody
    @RequestMapping(value = "/selectClubActivityWrapper", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索社团活动信息", notes = "搜索社团活动信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索社团活动信息",url="/clubActivity/selectClubActivityWrapper")
    public ResponseListModel<ClubActivity> selectClubActivityWrapper(@ApiParam(required = true, name = "ClubActivityInsertVO")
                                                                     @RequestBody ClubActivity clubActivity) {
//        return new ResponseListModel<>(clubActivityService.list(new QueryWrapper<ClubActivity>().and(warpper -> warpper.like("title",clubActivity.getTitle()).or().like("content",clubActivity.getContent()))));

        return null;
    }

    @ResponseBody
    @RequestMapping(value = "/selectMyClubActivityWrapper", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索社团活动信息", notes = "搜索社团活动信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索社团活动信息",url="/clubActivity/selectMyClubActivityWrapper")
    public ResponseListModel<ClubActivity> selectMyClubActivityWrapper(@ApiParam(required = true, name = "ClubActivity")
                                                                       @RequestBody ClubActivity clubActivity) {
//        return new ResponseListModel<>(clubActivityService.list(new QueryWrapper<ClubActivity>().and(warpper -> warpper.like("content",clubActivity.getContent()))));
        return null;
    }



    @ResponseBody
    @RequestMapping(value = "/selectListClubActivity", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询社团活动信息", notes = "查询社团活动信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询社团活动信息",url="/clubActivity/selectListClubActivity")
    public ResponseListModel<ClubActivityViewVO> selectListClubActivity() {

//        return new ResponseListModel<>(clubActivityService.selectListClubActivity());
        return null;
    }



}
